package com.mess.warningmess;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 
 * @author dell 向指定号码发送指定内容指定签名的报警通知消息
 *
 *
 */
public class WarningMass {
	
	private static Logger logger = LogManager.getLogger(WarningMass.class);
	/**
	 * 
	 * @param templateCode
	 *            模板编号
	 * @param sign
	 *            签名
	 * @param param
	 *            传入要发送的参数
	 * @param telNum
	 *            要发送的号码
	 * @return 返回请求Id
	 */

	public static String sendMessBase(Map<String, String> param, String tel) {
    	final String accessKeyId = "LTAIRfYaEQ6VQMQy";
        final String accessKeySecret = "uSIHGhSA9khdAj6U5ilWggZ2jb3ltZ";
        
        //产品名称:云通信短信API产品,开发者无需替换
        final String product = "Dysmsapi";
        //产品域名,开发者无需替换
        final String domain = "dysmsapi.aliyuncs.com";
        
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        
        //map转换成json字符串
        StringBuffer buff = new StringBuffer();
		buff.append("{");
		for (Map.Entry<String, String> key : param.entrySet()) {
			buff.append("\"" + key.getKey() + "\":\"" + key.getValue() + "\",");
		}
		buff.delete(buff.length() - 1, buff.length());
		buff.append("}");
        
        //hint 此处可能会抛出异常，注意catch
		SendSmsResponse sendSmsResponse = null;
		try {
			//初始化acsClient,暂不支持region化
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);

			//组装请求对象-具体描述见控制台-文档部分内容
			SendSmsRequest request = new SendSmsRequest();
			//必填:待发送手机号
			request.setPhoneNumbers(tel);
			//必填:短信签名-可在短信控制台中找到
			request.setSignName("智能物联");
			//必填:短信模板-可在短信控制台中找到
			request.setTemplateCode("SMS_113215021");
			//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
			request.setTemplateParam(buff.toString());

			sendSmsResponse = acsClient.getAcsResponse(request);
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		
        return sendSmsResponse==null?null:sendSmsResponse.getRequestId();
    }
	//发送消息
	public static String sendMess(Bean4WarnMessInfo info) {
		Map<String, String> param = new HashMap<>();
		// 发送警报时间
		param.put("time", currentTime());
		// 获取设备编号
		param.put("number",info.getID());
		// 判断设备类型
		param.put("type", info.getType());
		// 写入设备位置
		param.put("position", info.getPosition());
		
		
		param.put("status", info.getFalut());
		param.put("operator", info.getOperator());
		//判断电话空
		if (info.getTel().equals("")||info.getTel().length()!=11) {
			return "-1";
		}
		logger.debug("设备SMS："+info.getID()+" 出现变化："+info.getFalut()+"发送短信给："+info.getTel());

		return WarningMass.sendMessBase(param, info.getTel());
	}
	
	public static void sendMess(List<Bean4WarnMessInfo> infos){
		infos.forEach(info->{
			sendMess(info);
		});
	}
	
	

	// 生成当前时间字符串
	private static String currentTime() {
		DateFormat format = new SimpleDateFormat("yy年MM月dd日HH:mm");
		return format.format(new Date());
	}
}
